<?php
/**
 * Created by PhpStorm.
 * User: niqiaoyun
 * Date: 2017/9/5
 * Time: 09:26
 */

namespace app\models;


use app\common\widgets\Tools;
use app\models\dao\Admin;
use app\models\dao\Role;
use yii\db\Expression;

class AdminModel extends Admin {

	public function getAll($query = [],$page = []) {
		$where = [];
		if(!empty($query['where'])) {
			$where = $query['where'];
		}

		$query_order = ['id'=>SORT_ASC];
		$query_wheres = ['and'];
		$query_wheres[] = " flag = 1";
		if (!empty($where['name'])) {
			$query_wheres[] = ['username'=>$where['name']];
		}
		if (!empty($where['role_id'])) {
			$query_wheres[] = ['role_id'=>$where['role_id']];
		}

		$offset = ($page['pageno'] - 1) * $page['pagesize'];
		$limit = $page['pagesize'];

		// 读取总条数
		$count = $this->count($query_wheres);

		// 计算分页
		$page = Tools::getPage($count,$page);
		$arr_rows = Admin::find()->with("role")->where($query_wheres)->orderBy($query_order)->limit($limit)->offset($offset)->asArray()->all();
		return Tools::formatPage($arr_rows, $count);
	}

	public function getOne($int_id) {
		$arr_rows = Admin::find()->where(['id' => $int_id, 'flag' => 1])->one();
		if(empty($arr_rows)) {
			return array();
		}
		return $arr_rows;
	}

	public function create($array_params) {
		$createObj = new Admin();

		$bool_save = $createObj->setData($array_params)->insert();
		if(true === $bool_save) {
			return array('status'=>true,'msg'=>'success','data'=>array('name'=>$array_params['username'],'create_time'=>$array_params['create_time']));
		}else {
			return array('status'=>false,'msg'=>'error');
		}
	}

	/**
	 * 更新行业分类信息
	 * @param array $data
	 */
	public function modify($data = []) {
		// 保存数据
		$int_id = (int)$data['id'];
		unset($data['id']);
		$modifyObj = new Admin();
		$update_result = $modifyObj->updateAll($data, 'id = :id',
			[':id'=>$int_id]);
		if ($update_result >= 0) {
			return true;
		} else {
			return false;
		}
	}


	public function count($where = []) {
		$count = Admin::find()->where($where)->asArray()->count(1);
		return $count;
	}

	public function CheckName($where) {
		$query_wheres = ['and'];
		$query_wheres[] = ['username'=>$where['username'], 'flag' => 1];
		if (!empty($where['id'])) {
			$query_wheres[] = ['<>','id', $where['id']];
		}

		$ary_result = Admin::find()->where($query_wheres)->one();
		if(empty($ary_result)) {
			return false;
		}
		return true;
	}

	public function modifyPk($pk, $attributes) {
		if(empty($pk)) {
			return false;
		}
		$params = [];
		$modifyObj = new Admin();
		foreach($pk as $key=>$val) {
			$params[':id'] = $val;
			$update_result = $modifyObj->updateAll($attributes, "id=:id", $params);
		}

		if ($update_result >= 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 编辑
	 * @author Terry<admin@huicms.cn>
	 * @date 2016-05-08
	 */
	public function edit($ary_data) {
		$int_rows = '-1';
		try {
			$int_id = $ary_data['id'];
			$ary_attributes['update_time'] = new Expression("NOW()");
			if (true == isset ( $ary_data ['countlog'] )) {
				$ary_attributes ['countlog'] = new Expression('countlog + :countlog',array(':countlog'=> 1));
			}

			if(true == isset( $ary_data['lastlogin_time'])) {
				$ary_attributes ['lastlogin_time'] = $ary_data ['lastlogin_time'];
			}

			if(true == isset($ary_data['username']) && false == empty($ary_data['username'])) {
				$ary_attributes ['username'] = $ary_data ['username'];
			}

			if(true == isset($ary_data['password']) && false == empty($ary_data['password'])) {
				$ary_attributes ['password'] = md5($ary_data ['password']);
			}

			if(true == isset($ary_data['nick']) && false == empty($ary_data['nick'])) {
				$ary_attributes ['nick'] = md5($ary_data ['nick']);
			}

			$str_condition = ' id=:id';
			$ary_params = [
					':id'=> $int_id,
			];
			$int_rows = $this->updateAll ( $ary_attributes, $str_condition, $ary_params );
		} catch ( \Exception $e ) {
			return $int_rows;
		}
		return $int_rows;
	}

	/**
	 * 新增账号
	 *
	 * @param array $ary_data
	 * @return boolean 添加成功返回true，失败返回false
	 * @author Terry<admin@Huicms.cn> 2016-12-01
	 * @version 1.0
	 */
	public function add($ary_data) {
		$bool_add = false;
		try {
			$ary_meeting_data = [
					'username'  => $ary_data['username'],
					'password'  => md5($ary_data['password']),
					'role_id'  => $ary_data['role_id'],
					'nick'  => $ary_data['nick'],
					'ip'  => Tools::getClientIP(),
					'create_time' => new Expression("NOW()"),
			];

			$obj_user_models = new Admin();
			$bool_add = $obj_user_models->setData ( $ary_meeting_data )->insert ();
			if (true == $bool_add) {
				$bool_add = $obj_user_models->id;
			}
		} catch ( \Exception $e ) {
			$bool_add = false;
		}
		return $bool_add;
	}

	/**
	 * 获取信息
	 *
	 * @param array|string $ary_condition
	 * @param string $str_fields
	 * @return array 产品信息
	 *
	 * @author Terry<admin@huicms.cn> 2016-12-02
	 * @version 1.0
	 */
	public function searchByOne($ary_condition, $str_fields  = "*") {
		$ary_result = [ ];
		try {
			$ary_result = $this->find ()->select ( $str_fields )->where ( $ary_condition )->orderBy ( 'create_time DESC' )->asArray()->one();
		} catch ( \Exception $e ) {
			$ary_result = [ ];
		}
		return $ary_result;
	}

	public function searchListByIds($ary_condition) {
		$ary_result = [];

		try {
			$ary_result = $this->find ()
					->select( '*' )
					->where ( $ary_condition )
					->asArray ()
					->all ();
		} catch (\Exception $e) {

		}
		return $ary_result;
	}
}